package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.db.User;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.mapper.UserMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    /**
     * 保存用户
     * @param user
     * @return  保存用户之后返回的用户的id
     */
    @Override
    public Long save(User user) {
        //这里返回值代表了影响的行数，我们需要得到是id主键
        userMapper.insert(user); //获取新增的主键
        return user.getId();
    }


    /**
     * 根据手机号查询用户
     * @param mobile
     * @return
     */
    @Override
    public User findByMobile(String mobile) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public List<User> findAll(){
        List<User> userList = userMapper.selectList(null);
        return userList;
    }

    @Override
    public void updateMobile(User user) {
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("id", user.getId());
        userMapper.update(user, query);
    }
}
